﻿using MobiSms.Enums;
using MobiSms.Infrastructure;
using System;
using System.Data.Linq.Mapping;

namespace MobiSms.DataModel
{
    [Table(Name = "T_Msgbody")]
    public class MessageInfo : NotificationObject
    {
        #region Fields
        private string _guid = System.Guid.NewGuid().ToString();
        private string _serialNumber;
        private string _sendid;
        private string _content = string.Empty;
        private string _extension;
        private DateTime _time;
        private DateTime _submitTime = DateTime.Now;
        private bool _isSign = true;
        private string _signature = string.Empty;
        private MessageStatus _status = MessageStatus.Faild;
        #endregion

        #region Properties
        [Column(IsPrimaryKey = true)]
        public string Guid
        {
            get { return _guid; }
            set
            {
                _guid = value;
                RaisePropertyChanged(() => Guid);
            }
        }

        [Column]
        public string SerialNumber
        {
            get { return _serialNumber; }
            set
            {
                _serialNumber = value;
                RaisePropertyChanged(() => SerialNumber);
            }
        }

        [Column]
        public string SendId
        {
            get { return _sendid; }
            set
            {
                _sendid = value;
                RaisePropertyChanged(() => SendId);
            }
        }

        [Column]
        public string Content
        {
            get { return _content; }
            set
            {
                _content = value;
                RaisePropertyChanged(() => Content);
            }
        }

        public string DisplayContent
        {
            get { return Content.Length > 35 ? Content.Substring(0, 35) + "..." : Content; }
        }

        [Column]
        public string Extension
        {
            get { return _extension; }
            set
            {
                _extension = value;
                RaisePropertyChanged(() => Extension);
            }
        }

        [Column]
        public DateTime Time
        {
            get { return _time; }
            set
            {
                _time = value;
                RaisePropertyChanged(() => Time);
            }
        }

        [Column]
        public bool IsSign
        {
            get { return _isSign; }
            set
            {
                _isSign = value;
                RaisePropertyChanged(() => IsSign);
            }
        }

        [Column]
        public string Signature
        {
            get { return _signature; }
            set
            {
                _signature = value;
                RaisePropertyChanged(() => Signature);
            }
        }

        [Column]
        public DateTime SubmitTime
        {
            get { return _submitTime; }
            set
            {
                _submitTime = value;
                RaisePropertyChanged(() => SubmitTime);
            }
        }

        [Column]
        public MessageStatus Status
        {
            get { return _status; }
            set
            {
                _status = value;
                RaisePropertyChanged(() => Status);
            }
        }
        #endregion

        #region Events
        public event EventHandler<PropertyArgs> ValueChanged = delegate { };

        protected override void RaisePropertyChanged(string propertyName)
        {
            base.RaisePropertyChanged(propertyName);
            ValueChanged.Invoke(this, new PropertyArgs()
            {
                Property = GetType().GetProperty(propertyName),
                Value = GetType().GetProperty(propertyName).GetValue(this, null)
            });
        }
        #endregion
    }
}
